package com.starsea.book.ui.newpage.weidget

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.res.ColorStateList
import android.content.res.Resources
import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.os.Build
import android.text.TextUtils
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.View.OnFocusChangeListener
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.blankj.utilcode.util.BarUtils
import com.starsea.book.R
import com.starsea.book.ui.newpage.utils.DrawableUtil
import com.starsea.book.ui.newpage.utils.StatusBarUtil
import com.starsea.book.ui.newpage.utils.ViewGroupUtils
import com.starsea.book.ui.newpage.utils.gone
import com.starsea.book.ui.newpage.utils.visible

/**
 * Function:定制标题栏
 * Description:
 */
class TitleBarView @JvmOverloads constructor(
  private val mContext: Context,
  attrs: AttributeSet? = null,
  defStyleAttr: Int = 0
) : ViewGroup(
  mContext, attrs, defStyleAttr
) {
  private var mStatusBarHeight = 0 //状态栏高度
  private var mScreenWidth = 0 //TitleBarView实际占用宽度

  /**
   * 自定义View
   */
  private var mVStatus: View? = null //状态栏View-用于单独设置颜色
  private var mLLayoutLeft: LinearLayout? = null //左边容器
  private var mLLayoutCenter: LinearLayout? = null //中间容器
  private var mLLayoutRight: LinearLayout? = null //右边容器
  private var mTvLeft: TextView? = null //左边TextView
  private var mTvLeftRight: TextView? = null //左边TextView
  private var mTvTitleMain: TextView? = null //主标题
  private var mTvTitleSub: TextView? = null //副标题
  private var mTvRight: TextView? = null //右边TextView
  private var mTvRightLeft: TextView? = null //右边TextView
  private var mVDivider: View? = null //下方下划线

  /**
   * 是否增加状态栏高度
   */
  private var mIsPlusStatusHeight = true
  /**
   * 获取设置状态栏文字图标样式模式
   *
   * @return >0则表示设置成功 参考[StatusBarUtil]
   */
  /**
   * 设置状态栏浅色或深色模式类型标记;>0则表示支持模式切换
   */
  var statusBarModeType: Int = StatusBarUtil.STATUS_BAR_TYPE_DEFAULT
    private set

  /**
   * xml属性
   */
  private var mImmersible = false
  private var mOutPadding = 0
  private var mActionPadding = 0
  private var mCenterLayoutPadding = 0 //中间部分是Layout左右padding
  private var mCenterGravityLeft = false //中间部分是否左对齐--默认居中
  private var mCenterGravityLeftPadding = 0 //中间部分左对齐是Layout左padding
  private var mStatusBarLightMode = true //是否浅色状态栏(黑色文字及图标)
  private var mStatusBackground: Drawable? = null
  private var mDividerBackground: Drawable? = null
  private var mDividerHeight = 0
  private var mDividerVisible = false
  private var mLeftText: CharSequence? = null
  private val mLeftRightText: CharSequence? = null
  private var mLeftTextSize = 0
  private var mLeftTextColor: ColorStateList? = null
  private var mLeftTextBackground: Drawable? = null
  private var mLeftTextDrawable: Drawable? = null
  private var mLeftTextDrawableWidth = 0
  private var mLeftTextDrawableHeight = 0
  private var mLeftTextDrawablePadding = 0
  private var mTitleMainText: CharSequence? = null
  private var mTitleMainTextSize = 0
  private var mTitleMainTextColor: ColorStateList? = null
  private var mTitleMainTextBackground: Drawable? = null
  private var mTitleMainTextFakeBold = false
  private var mTitleMainTextMarquee = false
  private var mTitleSubText: CharSequence? = null
  private var mTitleSubTextSize = 0
  private var mTitleSubTextColor: ColorStateList? = null
  private var mTitleSubTextBackground: Drawable? = null
  private var mTitleSubTextFakeBold = false
  private var mTitleSubTextMarquee = false
  private var mRightText: CharSequence? = null
  private var mRightTextSize = 0
  private var mRightTextColor: ColorStateList? = null
  private var mRightTextBackground: Drawable? = null
  private var mRightTextDrawable: Drawable? = null
  private var mRightLeftTextDrawable: Drawable? = null
  private var mRightTextDrawableWidth = 0
  private var mRightTextDrawableHeight = 0
  private var mRightTextDrawablePadding = 0
  private var mActionTextSize = 0
  private var mActionTextColor: ColorStateList? = null
  private var mActionTextBackground: Drawable? = null
  private var mTitleContainerRect: Rect? = null

  init {
    initAttributes(mContext, attrs)
    initView(mContext)
    setViewAttributes(mContext)
  }

  private fun initAttributes(context: Context, attrs: AttributeSet?) {
    val ta = context.obtainStyledAttributes(attrs, R.styleable.TitleBarView)
    mImmersible = ta.getBoolean(R.styleable.TitleBarView_title_immersible, true)
    mOutPadding = ta.getDimensionPixelSize(
      R.styleable.TitleBarView_title_outPadding, dip2px(
        DEFAULT_OUT_PADDING
      )
    )
    mActionPadding =
      ta.getDimensionPixelSize(R.styleable.TitleBarView_title_actionPadding, dip2px(1f))
    mCenterLayoutPadding =
      ta.getDimensionPixelSize(R.styleable.TitleBarView_title_centerLayoutPadding, dip2px(2f))

    mCenterGravityLeft = ta.getBoolean(R.styleable.TitleBarView_title_centerGravityLeft, false)
    mCenterGravityLeftPadding = ta.getDimensionPixelSize(
      R.styleable.TitleBarView_title_centerGravityLeftPadding, dip2px(
        DEFAULT_CENTER_GRAVITY_LEFT_PADDING
      )
    )
    mStatusBarLightMode = ta.getBoolean(R.styleable.TitleBarView_title_statusBarLightMode, false)
    mStatusBackground = ta.getDrawable(R.styleable.TitleBarView_title_statusBackground)
    mDividerBackground = ta.getDrawable(R.styleable.TitleBarView_title_dividerBackground)
    mDividerHeight =
      ta.getDimensionPixelSize(R.styleable.TitleBarView_title_dividerHeight, dip2px(0.5f))
    mDividerVisible = ta.getBoolean(R.styleable.TitleBarView_title_dividerVisible, true)
    mLeftText = ta.getString(R.styleable.TitleBarView_title_leftText)
    mLeftTextSize = ta.getDimensionPixelSize(
      R.styleable.TitleBarView_title_leftTextSize, dip2px(
        DEFAULT_TEXT_SIZE
      )
    )
    mLeftTextColor = ta.getColorStateList(R.styleable.TitleBarView_title_leftTextColor)
    mLeftTextBackground = ta.getDrawable(R.styleable.TitleBarView_title_leftTextBackground)
    mLeftTextDrawable = ta.getDrawable(R.styleable.TitleBarView_title_leftTextDrawable)
    mLeftTextDrawableWidth =
      ta.getDimensionPixelSize(R.styleable.TitleBarView_title_leftTextDrawableWidth, -1)
    mLeftTextDrawableHeight =
      ta.getDimensionPixelSize(R.styleable.TitleBarView_title_leftTextDrawableHeight, -1)
    mLeftTextDrawablePadding =
      ta.getDimensionPixelSize(R.styleable.TitleBarView_title_leftTextDrawablePadding, dip2px(1f))
    mTitleMainText = ta.getString(R.styleable.TitleBarView_title_titleMainText)
    mTitleMainTextSize = ta.getDimensionPixelSize(
      R.styleable.TitleBarView_title_titleMainTextSize, dip2px(
        DEFAULT_MAIN_TEXT_SIZE
      )
    )
    mTitleMainTextColor = ta.getColorStateList(R.styleable.TitleBarView_title_titleMainTextColor)
    mTitleMainTextBackground =
      ta.getDrawable(R.styleable.TitleBarView_title_titleMainTextBackground)
    mTitleMainTextFakeBold =
      ta.getBoolean(R.styleable.TitleBarView_title_titleMainTextFakeBold, false)
    mTitleMainTextMarquee =
      ta.getBoolean(R.styleable.TitleBarView_title_titleMainTextMarquee, false)
    mTitleSubText = ta.getString(R.styleable.TitleBarView_title_titleSubText)
    mTitleSubTextSize = ta.getDimensionPixelSize(
      R.styleable.TitleBarView_title_titleSubTextSize, dip2px(
        DEFAULT_SUB_TEXT_SIZE
      )
    )
    mTitleSubTextColor = ta.getColorStateList(R.styleable.TitleBarView_title_titleSubTextColor)
    mTitleSubTextBackground = ta.getDrawable(R.styleable.TitleBarView_title_titleSubTextBackground)
    mTitleSubTextFakeBold =
      ta.getBoolean(R.styleable.TitleBarView_title_titleSubTextFakeBold, false)
    mTitleSubTextMarquee = ta.getBoolean(R.styleable.TitleBarView_title_titleSubTextMarquee, false)
    mRightText = ta.getString(R.styleable.TitleBarView_title_rightText)
    mRightTextSize = ta.getDimensionPixelSize(
      R.styleable.TitleBarView_title_rightTextSize, dip2px(
        DEFAULT_TEXT_SIZE
      )
    )
    mRightTextColor = ta.getColorStateList(R.styleable.TitleBarView_title_rightTextColor)
    mRightTextBackground = ta.getDrawable(R.styleable.TitleBarView_title_rightTextBackground)
    mRightTextDrawable = ta.getDrawable(R.styleable.TitleBarView_title_rightTextDrawable)
    mRightLeftTextDrawable = ta.getDrawable(R.styleable.TitleBarView_title_rightTextLeftDrawable)
    mRightTextDrawableWidth =
      ta.getDimensionPixelSize(R.styleable.TitleBarView_title_rightTextDrawableWidth, -1)
    mRightTextDrawableHeight =
      ta.getDimensionPixelSize(R.styleable.TitleBarView_title_rightTextDrawableHeight, -1)
    mRightTextDrawablePadding =
      ta.getDimensionPixelSize(R.styleable.TitleBarView_title_rightTextDrawablePadding, dip2px(1f))
    mActionTextSize = ta.getDimensionPixelSize(
      R.styleable.TitleBarView_title_actionTextSize, dip2px(
        DEFAULT_TEXT_SIZE
      )
    )
    mActionTextColor = ta.getColorStateList(R.styleable.TitleBarView_title_actionTextColor)
    mActionTextBackground = ta.getDrawable(R.styleable.TitleBarView_title_actionTextBackground)
    ta.recycle() //回收
  }

  /**
   * 初始化子View
   *
   * @param context
   */
  private fun initView(context: Context) {
    val params = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)
    val dividerParams = LayoutParams(LayoutParams.MATCH_PARENT, mDividerHeight)
    mLLayoutLeft = LinearLayout(context)
    mLLayoutCenter = LinearLayout(context)
    mLLayoutRight = LinearLayout(context)
    mVStatus = View(context)
    mVDivider = View(context)
    mLLayoutLeft!!.gravity = Gravity.CENTER_VERTICAL
    mLLayoutCenter!!.orientation = LinearLayout.VERTICAL
    mLLayoutRight!!.gravity = Gravity.CENTER_VERTICAL
    mTvLeft = TextView(context)
    mTvLeft!!.gravity = Gravity.CENTER
    mTvLeft!!.setLines(1)
    mTvLeftRight = TextView(context)
    mTvLeftRight!!.gravity = Gravity.CENTER
    mTvLeftRight!!.setLines(1)
    mTvTitleMain = TextView(context)
    mTvTitleSub = TextView(context)
    mTvRight = TextView(context)
    mTvRight!!.gravity = Gravity.CENTER
    mTvRight!!.setLines(1)
    mTvRightLeft = TextView(context)
    mTvRightLeft!!.gravity = Gravity.CENTER
    mTvRightLeft!!.setLines(1)
    mLLayoutLeft!!.addView(mTvLeft, params)
    mLLayoutLeft!!.addView(mTvLeftRight, params)
    mLLayoutRight!!.addView(mTvRightLeft, params)
    mLLayoutRight!!.addView(mTvRight, params)
    addView(mLLayoutLeft, params) //添加左边容器
    addView(mLLayoutCenter, params) //添加中间容器
    addView(mLLayoutRight, params) //添加右边容器
    addView(mVDivider, dividerParams) //添加下划线View
    addView(mVStatus) //添加状态栏View
  }

  /**
   * 设置xml默认属性
   *
   * @param context
   */
  private fun setViewAttributes(context: Context) {
    mScreenWidth = measuredWidth
    mStatusBarHeight = statusBarHeight
    if (context is Activity) {
      setImmersible(context, mImmersible)
      if (mStatusBarLightMode) {
        setStatusBarLightMode(mStatusBarLightMode)
      }
    }
    setOutPadding(mOutPadding)
    setActionPadding(mActionPadding)
    setCenterLayoutPadding(mCenterLayoutPadding)
    setCenterGravityLeft(mCenterGravityLeft)
    setStatusBackground(mStatusBackground)
    setDividerBackground(mDividerBackground)
    setDividerHeight(mDividerHeight)
    setDividerVisible(mDividerVisible)
    setLeftText(mLeftText)
    setLeftTextSize(TypedValue.COMPLEX_UNIT_PX, mLeftTextSize.toFloat())
    setLeftTextColor(mLeftTextColor)
    setLeftTextBackground(mLeftTextBackground)
    setLeftTextDrawable(mLeftTextDrawable)
    setLeftTextDrawableWidth(mLeftTextDrawableWidth)
    setLeftTextDrawableHeight(mLeftTextDrawableHeight)
    setLeftTextDrawablePadding(mLeftTextDrawablePadding)
    setTitleMainText(mTitleMainText)
    setTitleMainTextSize(TypedValue.COMPLEX_UNIT_PX, mTitleMainTextSize.toFloat())
    setTitleMainTextColor(mTitleMainTextColor)
    setTitleMainTextBackground(mTitleMainTextBackground)
    setTitleMainTextFakeBold(mTitleMainTextFakeBold)
    setTitleMainTextMarquee(mTitleMainTextMarquee)
    setTitleSubText(mTitleSubText)
    setTitleSubTextSize(TypedValue.COMPLEX_UNIT_PX, mTitleSubTextSize.toFloat())
    setTitleSubTextColor(mTitleSubTextColor)
    setTitleSubTextBackground(mTitleSubTextBackground)
    setTitleSubTextFakeBold(mTitleSubTextFakeBold)
    setTitleSubTextMarquee(mTitleSubTextMarquee)
    setRightText(mRightText)
    setRightTextSize(TypedValue.COMPLEX_UNIT_PX, mRightTextSize.toFloat())
    setRightTextColor(mRightTextColor)
    setRightTextBackground(mRightTextBackground)
    setRightTextDrawable(mRightTextDrawable)
    setRightTextDrawableWidth(mRightTextDrawableWidth)
    setRightTextDrawableHeight(mRightTextDrawableHeight)
    setRightTextDrawablePadding(mRightTextDrawablePadding)
    setRightLeftTextSize(TypedValue.COMPLEX_UNIT_PX, mRightTextSize.toFloat())
    setRightLeftTextColor(mRightTextColor)
    setRightLeftTextBackground(mRightTextBackground)
    setRightLeftTextDrawable(mRightLeftTextDrawable)
    setRightLeftTextDrawableWidth(mRightTextDrawableWidth)
    setRightLeftTextDrawableHeight(mRightTextDrawableHeight)
    setRightLeftTextDrawablePadding(mRightTextDrawablePadding)
  }

  val titleContainerRect: Rect
    get() {
      if (mTitleContainerRect == null) {
        mTitleContainerRect = Rect()
      }
      if (mLLayoutCenter == null) {
        mTitleContainerRect!![0, 0, 0] = 0
      } else {
        ViewGroupUtils.getDescendantRect(this, mLLayoutCenter!!, mTitleContainerRect!!)
      }
      mTitleContainerRect!![mTitleContainerRect!!.left + mLLayoutCenter!!.paddingLeft, mTitleContainerRect!!.top, mTitleContainerRect!!.right] =
        mTitleContainerRect!!.bottom
      return mTitleContainerRect as Rect
    }

  /**
   * 根据位置获取 LinearLayout
   *
   * @param gravity 参考[Gravity]
   * @return
   */
  fun getLinearLayout(gravity: Int): LinearLayout? {
    if (gravity == Gravity.LEFT || gravity == Gravity.START) {
      return mLLayoutLeft
    } else if (gravity == Gravity.CENTER) {
      return mLLayoutCenter
    } else if (gravity == Gravity.END || gravity == Gravity.RIGHT) {
      return mLLayoutRight
    }
    return mLLayoutCenter
  }

  /**
   * 根据位置获取TextView
   *
   * @param gravity 参考[Gravity]
   * @return
   */
  fun getTextView(gravity: Int): TextView? {
    if (gravity == Gravity.LEFT || gravity == Gravity.START) {
      return mTvLeft
    } else if (gravity == Gravity.CENTER or Gravity.TOP) {
      return mTvTitleMain
    } else if (gravity == Gravity.CENTER or Gravity.BOTTOM) {
      return mTvTitleSub
    } else if (gravity == Gravity.END || gravity == Gravity.RIGHT) {
      return mTvRight
    }
    return mTvTitleMain
  }

  /**
   * 根据位置获取View
   *
   * @param gravity 参考[Gravity]
   * @return
   */
  fun getView(gravity: Int): View? {
    if (gravity == Gravity.TOP) {
      return mVStatus
    } else if (gravity == Gravity.BOTTOM) {
      return mVDivider
    }
    return mVStatus
  }

  fun setImmersible(activity: Activity?, immersible: Boolean): TitleBarView {
    return setImmersible(activity, immersible, true)
  }

  fun setImmersible(
    activity: Activity?,
    immersible: Boolean,
    isTransStatusBar: Boolean
  ): TitleBarView {
    return setImmersible(activity, immersible, isTransStatusBar, true)
  }

  /**
   * 设置沉浸式状态栏，4.4以上系统支持
   *
   * @param activity
   * @param immersible       是否沉浸
   * @param isTransStatusBar 是否透明状态栏 --xml未设置statusBackground 属性才会执行
   * @param isPlusStatusBar  是否增加状态栏高度--用于控制底部有输入框 (设置false/xml背景色必须保持和状态栏一致)
   */
  fun setImmersible(
    activity: Activity?,
    immersible: Boolean,
    isTransStatusBar: Boolean,
    isPlusStatusBar: Boolean
  ): TitleBarView {
    mImmersible = immersible
    mIsPlusStatusHeight = isPlusStatusBar
    mStatusBarHeight = needStatusBarHeight
    if (activity == null) {
      return this
    }
    //透明状态栏
    val window = activity.window
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      mVStatus!!.layoutParams =
        LayoutParams(LayoutParams.MATCH_PARENT, mStatusBarHeight)
      // 透明状态栏
      window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.decorView.systemUiVisibility = (window.decorView.systemUiVisibility
            or SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.statusBarColor = Color.TRANSPARENT
      }
    }
    //        if (mStatusBackground == null) {
    setStatusAlpha(if (immersible) if (isTransStatusBar) 0 else 102 else 255)
    //        }
    return this
  }

  override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
    //实时获取避免因横竖屏切换造成测量错误
    mScreenWidth = measuredWidth
    mStatusBarHeight = needStatusBarHeight
    val left = mLLayoutLeft!!.measuredWidth
    val right = mLLayoutRight!!.measuredWidth
    val center = mLLayoutCenter!!.measuredWidth
    mLLayoutLeft!!.layout(
      0,
      if (isNormalParent) mStatusBarHeight else mStatusBarHeight / 2,
      left,
      mLLayoutLeft!!.measuredHeight + mStatusBarHeight
    )
    mLLayoutRight!!.layout(
      mScreenWidth - right,
      if (isNormalParent) mStatusBarHeight else mStatusBarHeight / 2,
      mScreenWidth,
      mLLayoutRight!!.measuredHeight + mStatusBarHeight
    )
    val isMuchScreen = left + right + center >= mScreenWidth
    if (left > right) {
      mLLayoutCenter!!.layout(
        left,
        mStatusBarHeight,
        if (isMuchScreen) mScreenWidth - right else mScreenWidth - left,
        measuredHeight - mDividerHeight
      )
      //            mLLayoutCenter.layout(left, mStatusBarHeight,mScreenWidth - right , getMeasuredHeight() - mDividerHeight);
    } else {
      mLLayoutCenter!!.layout(
        if (isMuchScreen) left else right,
        mStatusBarHeight,
        mScreenWidth - right,
        measuredHeight - mDividerHeight
      )
    }
    mVDivider!!.layout(
      0,
      measuredHeight - mVDivider!!.measuredHeight,
      measuredWidth,
      measuredHeight
    )
    mVStatus!!.layout(0, 0, measuredWidth, mStatusBarHeight)
  }

  override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
    mStatusBarHeight = needStatusBarHeight
    measureChild(mLLayoutLeft, widthMeasureSpec, heightMeasureSpec)
    measureChild(mLLayoutRight, widthMeasureSpec, heightMeasureSpec)
    measureChild(mLLayoutCenter, widthMeasureSpec, heightMeasureSpec)
    measureChild(mVDivider, widthMeasureSpec, heightMeasureSpec)
    measureChild(mVStatus, widthMeasureSpec, heightMeasureSpec)
    //重新测量宽高--增加状态栏及下划线的高度
    setMeasuredDimension(
      MeasureSpec.getSize(widthMeasureSpec),
      MeasureSpec.getSize(heightMeasureSpec) + (if (isNormalParent) mStatusBarHeight else mStatusBarHeight / 2) + mDividerHeight
    )
    mScreenWidth = measuredWidth
    val left = mLLayoutLeft!!.measuredWidth
    val right = mLLayoutRight!!.measuredWidth
    var center = mLLayoutCenter!!.measuredWidth
    //判断左、中、右实际占用宽度是否等于或者超过屏幕宽度
    val isMuchScreen = left + right + center >= mScreenWidth
    if (!mCenterGravityLeft) { //不设置中间布局左对齐才进行中间布局重新测量
      center = if (isMuchScreen) {
        mScreenWidth - left - right
      } else {
        if (left > right) {
          mScreenWidth - 2 * left
        } else {
          mScreenWidth - 2 * right
        }
      }
      mLLayoutCenter!!.measure(
        MeasureSpec.makeMeasureSpec(center, MeasureSpec.EXACTLY),
        heightMeasureSpec
      )
    }
  }

  /**
   * 设置TitleBarView高度--不包含状态栏及下划线
   *
   * @param height
   * @return
   */
  fun setHeight(height: Int): TitleBarView {
    val params = layoutParams
    if (params != null) {
      params.height = height
    }
    return this
  }

  fun setBgDrawable(background: Drawable?): TitleBarView {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
      setBackground(background)
    } else {
      setBackgroundDrawable(background)
    }
    return this
  }

  fun setBgColor(color: Int): TitleBarView {
    setBackgroundColor(color)
    return this
  }

  fun setBgResource(res: Int): TitleBarView {
    super.setBackgroundResource(res)
    return this
  }

  fun setOutPadding(paddingValue: Int): TitleBarView {
    mOutPadding = paddingValue
    mLLayoutLeft!!.setPadding(mOutPadding, 0, 0, 0)
    mLLayoutRight!!.setPadding(0, 0, mOutPadding, 0)
    return this
  }

  fun setCenterLayoutPadding(centerLayoutPadding: Int): TitleBarView {
    mCenterLayoutPadding = centerLayoutPadding
    mLLayoutCenter!!.setPadding(
      mCenterLayoutPadding,
      mLLayoutCenter!!.top,
      mCenterLayoutPadding,
      mLLayoutCenter!!.paddingBottom
    )
    return this
  }

  fun setCenterGravityLeft(enable: Boolean): TitleBarView {
    mCenterGravityLeft = enable
    mTvTitleMain!!.gravity = if (mCenterGravityLeft) Gravity.LEFT else Gravity.CENTER
    mLLayoutCenter!!.gravity =
      if (mCenterGravityLeft) Gravity.LEFT or Gravity.CENTER_VERTICAL else Gravity.CENTER
    mTvTitleSub!!.gravity = if (mCenterGravityLeft) Gravity.LEFT else Gravity.CENTER
    return setCenterGravityLeftPadding(mCenterGravityLeftPadding)
  }

  /**
   * 设置title 左边距--当设置setCenterGravityLeft(true)生效
   *
   * @param padding
   * @return
   */
  fun setCenterGravityLeftPadding(padding: Int): TitleBarView {
    if (mCenterGravityLeft) {
      mCenterGravityLeftPadding = padding
      mLLayoutCenter!!.setPadding(
        mCenterGravityLeftPadding,
        mLLayoutCenter!!.top,
        mLLayoutCenter!!.paddingRight,
        mLLayoutCenter!!.paddingBottom
      )
    } else {
      return setCenterLayoutPadding(mCenterLayoutPadding)
    }
    return this
  }

  fun setStatusBarLightMode(mStatusBarLightMode: Boolean): TitleBarView {
    return if (mContext is Activity) {
      setStatusBarLightMode(mContext, mStatusBarLightMode)
    } else this
  }

  /**
   * 设置状态栏文字黑白颜色切换
   *
   * @param mActivity
   * @param mStatusBarLightMode
   * @return
   */
  fun setStatusBarLightMode(mActivity: Activity?, mStatusBarLightMode: Boolean): TitleBarView {
    this.mStatusBarLightMode = mStatusBarLightMode
    if (mActivity != null) {
      if (mStatusBarLightMode) {
        statusBarModeType = StatusBarUtil.setStatusBarLightMode(mActivity)
//        BarUtils.setStatusBarLightMode(mActivity, false)
      } else {
        statusBarModeType = StatusBarUtil.setStatusBarDarkMode(mActivity)
//        BarUtils.setStatusBarLightMode(mActivity, false)
      }
    }
    return this
  }

  val isStatusBarLightModeEnable: Boolean
    /**
     * 返回是否支持状态栏颜色切换
     *
     * @return
     */
    get() = StatusBarUtil.isSupportStatusBarFontChange

  /**
   * 设置view左右两边内边距
   *
   * @param actionPadding
   * @return
   */
  fun setActionPadding(actionPadding: Int): TitleBarView {
    mActionPadding = actionPadding
    return this
  }

  /**
   * 设置状态栏背景
   *
   * @param drawable
   * @return
   */
  fun setStatusBackground(drawable: Drawable?): TitleBarView {
    mStatusBackground = drawable
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
      mVStatus!!.background = drawable
    } else {
      mVStatus!!.setBackgroundDrawable(drawable)
    }
    return this
  }

  /**
   * 设置状态栏颜色
   *
   * @param color
   */
  fun setStatusBackgroundColor(color: Int): TitleBarView {
    return setStatusBackground(ColorDrawable(color))
  }

  /**
   * 设置透明度
   *
   * @param alpha
   * @return
   */
  fun setStatusAlpha(alpha: Int): TitleBarView {
    var alpha = alpha
    if (alpha < 0) {
      alpha = 0
    } else if (alpha > 255) {
      alpha = 255
    }
    return setStatusBackgroundColor(Color.argb(alpha, 0, 0, 0))
  }

  fun setStatusBackgroundResource(resource: Int): TitleBarView {
    var drawable: Drawable? = null
    try {
      drawable = resources.getDrawable(resource)
    } catch (e: Exception) {
    }
    return setStatusBackground(drawable)
  }

  /**
   * 设置下划线背景
   *
   * @param drawable
   * @return
   */
  fun setDividerBackground(drawable: Drawable?): TitleBarView {
    mDividerBackground = drawable
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
      mVDivider!!.background = drawable
    } else {
      mVDivider!!.setBackgroundDrawable(drawable)
    }
    return this
  }

  fun setDividerBackgroundColor(color: Int): TitleBarView {
    return setDividerBackground(ColorDrawable(color))
  }

  fun setDividerBackgroundResource(resource: Int): TitleBarView {
    var drawable: Drawable? = null
    try {
      drawable = resources.getDrawable(resource)
    } catch (e: Exception) {
    }
    return setDividerBackground(drawable)
  }

  fun setDividerHeight(dividerHeight: Int): TitleBarView {
    mDividerHeight = dividerHeight
    mVDivider!!.layoutParams.height = dividerHeight
    return this
  }

  fun setDividerVisible(visible: Boolean): TitleBarView {
    mDividerVisible = visible
    mVDivider!!.visibility = if (visible) VISIBLE else GONE
    return this
  }

  /**
   * 设置所有TextView的文本颜色--注意和其它方法的先后顺序
   *
   * @param color
   * @return
   */
  fun setTextColor(color: Int): TitleBarView {
    return setLeftTextColor(color)
      .setTitleMainTextColor(color)
      .setTitleSubTextColor(color)
      .setRightTextColor(color)
      .setActionTextColor(color)
  }

  fun setTextColor(colors: ColorStateList?): TitleBarView {
    return setLeftTextColor(colors)
      .setTitleMainTextColor(colors)
      .setTitleSubTextColor(colors)
      .setRightTextColor(colors)
      .setActionTextColor(colors)
  }

  fun setLeftText(title: CharSequence?): TitleBarView {
    mLeftText = title
    mTvLeft!!.text = title
    return this
  }

  fun setLeftRightText(title: CharSequence?): TitleBarView {
    mTvLeftRight!!.text = title
    return this
  }

  fun setLeftText(id: Int): TitleBarView {
    return setLeftText(resources.getText(id))
  }

  /**
   * 设置文字大小
   *
   * @param unit 文字单位[TypedValue]
   * @param size
   * @return
   */
  fun setLeftTextSize(unit: Int, size: Float): TitleBarView {
    mTvLeft!!.setTextSize(unit, size)
    return this
  }

  fun setLeftRightTextSize(unit: Int, size: Float): TitleBarView {
    mTvLeftRight!!.setTextSize(unit, size)
    return this
  }

  fun setLeftTextSize(size: Float): TitleBarView {
    return setLeftTextSize(TypedValue.COMPLEX_UNIT_SP, size)
  }

  fun setLeftRightTextSize(size: Float): TitleBarView {
    return setLeftRightTextSize(TypedValue.COMPLEX_UNIT_SP, size)
  }

  fun setLeftTextColor(color: Int): TitleBarView {
    mTvLeft!!.setTextColor(color)
    return this
  }

  fun setLeftRightTextColor(color: Int): TitleBarView {
    mTvLeftRight!!.setTextColor(color)
    return this
  }

  /**
   * 设置文字状态颜色-如按下颜色变化
   *
   * @param colors
   * @return
   */
  fun setLeftTextColor(colors: ColorStateList?): TitleBarView {
    if (colors != null) {
      mTvLeft!!.setTextColor(colors)
    } else {
      setLeftTextColor(DEFAULT_TEXT_COLOR)
    }
    return this
  }

  fun setLeftTextBackground(drawable: Drawable?): TitleBarView {
    mLeftTextBackground = drawable
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
      mTvLeft!!.background = drawable
    } else {
      mTvLeft!!.setBackgroundDrawable(drawable)
    }
    return this
  }

  fun setLeftTextBackgroundColor(color: Int): TitleBarView {
    return setLeftTextBackground(ColorDrawable(color))
  }

  /**
   * @param resId
   */
  fun setLeftTextBackgroundResource(resId: Int): TitleBarView {
    var drawable: Drawable? = null
    try {
      drawable = resources.getDrawable(resId)
    } catch (e: Exception) {
    }
    return setLeftTextBackground(drawable)
  }

  /**
   * 设置左边图片资源
   *
   * @param drawable
   * @return
   */
  fun setLeftTextDrawable(drawable: Drawable?): TitleBarView {
    mLeftTextDrawable = drawable
    DrawableUtil.setDrawableWidthHeight(
      mLeftTextDrawable,
      mLeftTextDrawableWidth,
      mLeftTextDrawableHeight
    )
    mTvLeft!!.setCompoundDrawables(mLeftTextDrawable, null, null, null)
    return this
  }

  fun setLeftTextDrawable(id: Int): TitleBarView {
    var drawable: Drawable? = null
    try {
      drawable = mContext.resources.getDrawable(id)
    } catch (e: Exception) {
    }
    return setLeftTextDrawable(drawable)
  }

  fun setLeftTextDrawableWidth(width: Int): TitleBarView {
    mLeftTextDrawableWidth = width
    return setLeftTextDrawable(mLeftTextDrawable)
  }

  fun setLeftTextDrawableHeight(height: Int): TitleBarView {
    mLeftTextDrawableHeight = height
    return setLeftTextDrawable(mLeftTextDrawable)
  }

  fun setLeftTextDrawablePadding(drawablePadding: Int): TitleBarView {
    mLeftTextDrawablePadding = drawablePadding
    mTvLeft!!.compoundDrawablePadding = mLeftTextDrawablePadding
    return this
  }

  fun setLeftTextPadding(left: Int, top: Int, right: Int, bottom: Int): TitleBarView {
    mTvLeft!!.setPadding(left, top, right, bottom)
    return this
  }

  fun setOnLeftTextClickListener(l: OnClickListener?): TitleBarView {
    mTvLeft!!.setOnClickListener(l)
    return this
  }

  fun setOnLeftRightTextClickListener(l: OnClickListener?): TitleBarView {
    mTvLeftRight!!.setOnClickListener(l)
    return this
  }

  fun setLeftVisible(visible: Boolean): TitleBarView {
    mTvLeft!!.visibility = if (visible) VISIBLE else GONE
    return this
  }

  fun setTitleMainText(id: Int): TitleBarView {
    return setTitleMainText(resources.getText(id))
  }

  fun setTitleMainText(charSequence: CharSequence?): TitleBarView {
    mTvTitleMain!!.text = charSequence

    if (!TextUtils.isEmpty(charSequence)
      && !hasChildView(
        mLLayoutCenter,
        mTvTitleMain
      )
    ) { //非空且还未添加主标题
      mTvTitleMain!!.visible()
      mLLayoutCenter!!.addView(mTvTitleMain, 0)
    } else if (!TextUtils.isEmpty(charSequence)

    ) {
      mTvTitleMain!!.visible()
    } else {
      mTvTitleMain!!.gone()
    }
    return this
  }

  /**
   * [TextView.setTextSize]
   *
   * @param unit
   * @param titleMainTextSpValue
   * @return
   */
  fun setTitleMainTextSize(unit: Int, titleMainTextSpValue: Float): TitleBarView {
    mTvTitleMain!!.setTextSize(unit, titleMainTextSpValue)
    return this
  }

  /**
   * 设置文字大小 参考[TypedValue]
   *
   * @param titleMainTextSpValue
   * @return
   */
  fun setTitleMainTextSize(titleMainTextSpValue: Float): TitleBarView {
    return setTitleMainTextSize(TypedValue.COMPLEX_UNIT_SP, titleMainTextSpValue)
  }

  fun setTitleMainTextColor(color: Int): TitleBarView {
    mTvTitleMain!!.setTextColor(color)
    return this
  }

  fun setTitleMainTextColor(colors: ColorStateList?): TitleBarView {
    if (colors != null) {
      mTvTitleMain!!.setTextColor(colors)
    } else {
      setTitleMainTextColor(DEFAULT_TEXT_COLOR)
    }
    return this
  }

  fun setTitleMainTextBackground(drawable: Drawable?): TitleBarView {
    mTitleMainTextBackground = drawable
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
      mTvTitleMain!!.background = mTitleMainTextBackground
    } else {
      mTvTitleMain!!.setBackgroundDrawable(mTitleMainTextBackground)
    }
    return this
  }

  fun setTitleMainTextBackgroundColor(color: Int): TitleBarView {
    return setTitleMainTextBackground(ColorDrawable(color))
  }

  fun setTitleMainTextBackgroundResource(resId: Int): TitleBarView {
    var drawable: Drawable? = null
    try {
      drawable = resources.getDrawable(resId)
    } catch (e: Exception) {
    }
    return setTitleMainTextBackground(drawable)
  }

  /**
   * 设置粗体标题
   *
   * @param isFakeBold
   */
  fun setTitleMainTextFakeBold(isFakeBold: Boolean): TitleBarView {
    mTitleMainTextFakeBold = isFakeBold
    mTvTitleMain!!.paint.isFakeBoldText = mTitleMainTextFakeBold
    return this
  }

  fun setTitleMainTextMarquee(enable: Boolean): TitleBarView {
    mTitleMainTextMarquee = enable
    if (enable) {
      setTitleSubTextMarquee(false)
      mTvTitleMain!!.setSingleLine()
      mTvTitleMain!!.ellipsize = TextUtils.TruncateAt.MARQUEE
      mTvTitleMain!!.isFocusable = true
      mTvTitleMain!!.isFocusableInTouchMode = true
      mTvTitleMain!!.requestFocus()
      mTvTitleMain!!.onFocusChangeListener = OnFocusChangeListener { v, hasFocus ->
        if (!hasFocus && mTitleMainTextMarquee) {
          mTvTitleMain!!.requestFocus()
        }
      }
      //开启硬件加速
      mTvTitleMain!!.setLayerType(LAYER_TYPE_HARDWARE, null)
    } else {
      mTvTitleMain!!.maxLines = 1
      mTvTitleMain!!.ellipsize = TextUtils.TruncateAt.END
      mTvTitleMain!!.onFocusChangeListener = null
      //关闭硬件加速
      mTvTitleMain!!.setLayerType(LAYER_TYPE_NONE, null)
    }
    return this
  }

  fun setTitleMainTextPadding(left: Int, top: Int, right: Int, bottom: Int): TitleBarView {
    mTvTitleMain!!.setPadding(left, top, right, bottom)
    return this
  }

  fun setTitleSubText(charSequence: CharSequence?): TitleBarView {
    if (charSequence == null || charSequence.toString().isEmpty()) {
      mTvTitleSub!!.visibility = GONE
    } else {
      mTvTitleSub!!.visibility = VISIBLE
    }
    mTvTitleSub!!.text = charSequence
    if (!TextUtils.isEmpty(charSequence) && !hasChildView(
        mLLayoutCenter,
        mTvTitleSub
      )
    ) { //非空且还未添加副标题
      if (hasChildView(mLLayoutCenter, mTvTitleMain)) {
        mTvTitleMain!!.setSingleLine()
        mTvTitleSub!!.setSingleLine()
      }
      mLLayoutCenter!!.addView(mTvTitleSub)
    }
    return this
  }

  fun setTitleSubText(id: Int): TitleBarView {
    return setTitleSubText(resources.getText(id))
  }

  /**
   * 设置文字大小
   *
   * @param unit  单位 参考[TypedValue]
   * @param value
   * @return
   */
  fun setTitleSubTextSize(unit: Int, value: Float): TitleBarView {
    mTvTitleSub!!.setTextSize(unit, value)
    return this
  }

  fun setTitleSubTextSize(spValue: Float): TitleBarView {
    return setTitleSubTextSize(TypedValue.COMPLEX_UNIT_SP, spValue)
  }

  fun setTitleSubTextColor(color: Int): TitleBarView {
    mTvTitleSub!!.setTextColor(color)
    return this
  }

  fun setTitleSubTextColor(colors: ColorStateList?): TitleBarView {
    if (colors != null) {
      mTvTitleSub!!.setTextColor(colors)
    } else {
      setTitleSubTextColor(DEFAULT_TEXT_COLOR)
    }
    return this
  }

  fun setTitleSubTextBackground(drawable: Drawable?): TitleBarView {
    mTitleSubTextBackground = drawable
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
      mTvTitleSub!!.background = drawable
    } else {
      mTvTitleSub!!.setBackgroundDrawable(drawable)
    }
    return this
  }

  fun setTitleSubTextBackgroundColor(color: Int): TitleBarView {
    return setTitleSubTextBackground(ColorDrawable(color))
  }

  fun setTitleSubTextBackgroundResource(resId: Int): TitleBarView {
    var drawable: Drawable? = null
    try {
      drawable = resources.getDrawable(resId)
    } catch (e: Exception) {
    }
    return setTitleSubTextBackground(drawable)
  }

  /**
   * 设置粗体标题
   *
   * @param isFakeBold
   */
  fun setTitleSubTextFakeBold(isFakeBold: Boolean): TitleBarView {
    mTitleSubTextFakeBold = isFakeBold
    mTvTitleSub!!.paint.isFakeBoldText = mTitleSubTextFakeBold
    return this
  }

  /**
   * 设置TextView 跑马灯
   *
   * @param enable
   */
  fun setTitleSubTextMarquee(enable: Boolean): TitleBarView {
    mTitleSubTextMarquee = enable
    if (enable) {
      setTitleMainTextMarquee(false)
      mTvTitleSub!!.setSingleLine()
      mTvTitleSub!!.ellipsize = TextUtils.TruncateAt.MARQUEE
      mTvTitleSub!!.isFocusable = true
      mTvTitleSub!!.isFocusableInTouchMode = true
      mTvTitleSub!!.requestFocus()
      mTvTitleSub!!.onFocusChangeListener = OnFocusChangeListener { v, hasFocus ->
        if (!hasFocus && mTitleSubTextMarquee) {
          mTvTitleMain!!.requestFocus()
        }
      }
      //开启硬件加速
      mTvTitleSub!!.setLayerType(LAYER_TYPE_HARDWARE, null)
    } else {
      mTvTitleSub!!.maxLines = 1
      mTvTitleSub!!.ellipsize = TextUtils.TruncateAt.END
      mTvTitleSub!!.onFocusChangeListener = null
      //关闭硬件加速
      mTvTitleSub!!.setLayerType(LAYER_TYPE_NONE, null)
    }
    return this
  }

  fun setOnCenterClickListener(l: OnClickListener?): TitleBarView {
    mLLayoutCenter!!.setOnClickListener(l)
    return this
  }

  fun setRightText(title: CharSequence?): TitleBarView {
    mTvRight!!.text = title
    return this
  }

  fun setRightLeftText(title: CharSequence?): TitleBarView {
    mTvRightLeft!!.text = title
    return this
  }

  fun setRightText(id: Int): TitleBarView {
    return setRightText(resources.getText(id))
  }

  /**
   * 设置文字大小
   *
   * @param unit 单位 参考[TypedValue]
   * @param size
   * @return
   */
  fun setRightTextSize(unit: Int, size: Float): TitleBarView {
    mTvRight!!.setTextSize(unit, size)
    return this
  }

  fun setRightLeftTextSize(unit: Int, size: Float): TitleBarView {
    mTvRightLeft!!.setTextSize(unit, size)
    return this
  }

  fun setRightTextSize(size: Float): TitleBarView {
    return setRightTextSize(TypedValue.COMPLEX_UNIT_SP, size)
  }

  fun setRightTextColor(color: Int): TitleBarView {
    mTvRight!!.setTextColor(color)
    return this
  }

  fun setRightLeftTextColor(color: Int): TitleBarView {
    mTvRightLeft!!.setTextColor(color)
    return this
  }

  fun setRightTextColor(colors: ColorStateList?): TitleBarView {
    if (colors != null) {
      mTvRight!!.setTextColor(colors)
    } else {
      setRightTextColor(DEFAULT_TEXT_COLOR)
    }
    return this
  }

  fun setRightLeftTextColor(colors: ColorStateList?): TitleBarView {
    if (colors != null) {
      mTvRightLeft!!.setTextColor(colors)
    } else {
      setRightTextColor(DEFAULT_TEXT_COLOR)
    }
    return this
  }

  fun setRightTextBackground(drawable: Drawable?): TitleBarView {
    mRightTextBackground = drawable
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
      mTvRight!!.background = drawable
    } else {
      mTvRight!!.setBackgroundDrawable(drawable)
    }
    return this
  }

  fun setRightLeftTextBackground(drawable: Drawable?): TitleBarView {
    mRightTextBackground = drawable
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
      mTvRightLeft!!.background = drawable
    } else {
      mTvRightLeft!!.setBackgroundDrawable(drawable)
    }
    return this
  }

  fun setRightTextBackgroundColor(color: Int): TitleBarView {
    return setRightTextBackground(ColorDrawable(color))
  }

  fun setRightTextBackgroundResource(resId: Int): TitleBarView {
    var drawable: Drawable? = null
    try {
      drawable = resources.getDrawable(resId)
    } catch (e: Exception) {
    }
    return setRightTextBackground(drawable)
  }

  /**
   * 右边文本添加图片
   *
   * @param drawable 资源
   */
  fun setRightTextDrawable(drawable: Drawable?): TitleBarView {
    mRightTextDrawable = drawable
    DrawableUtil.setDrawableWidthHeight(
      mRightTextDrawable,
      mRightTextDrawableWidth,
      mRightTextDrawableHeight
    )
    mTvRight!!.setCompoundDrawablesWithIntrinsicBounds(null, null, mRightTextDrawable, null)
    return this
  }

  fun setRightTextLeftDrawable(drawable: Drawable?, drawable1: Drawable?): TitleBarView {
    DrawableUtil.setDrawableWidthHeight(
      mRightTextDrawable,
      mRightTextDrawableWidth,
      mRightTextDrawableHeight
    )
    mTvRight!!.setCompoundDrawablesWithIntrinsicBounds(drawable, null, drawable1, null)
    return this
  }

  /**
   * 右边文本添加图片
   *
   * @param drawable 资源
   */
  fun setRightLeftTextDrawable(drawable: Drawable?): TitleBarView {
    mRightLeftTextDrawable = drawable
    DrawableUtil.setDrawableWidthHeight(
      mRightLeftTextDrawable,
      mRightTextDrawableWidth,
      mRightTextDrawableHeight
    )
    mTvRightLeft!!.setCompoundDrawablesWithIntrinsicBounds(null, null, mRightLeftTextDrawable, null)
    return this
  }

  fun setRightTextDrawable(id: Int): TitleBarView {
    var drawable: Drawable? = null
    try {
      drawable = mContext.resources.getDrawable(id)
    } catch (e: Exception) {
    }
    return setRightTextDrawable(drawable)
  }

  fun setRightTextLeftDrawable(id: Int): TitleBarView {
    var drawable: Drawable? = null
    try {
      drawable = mContext.resources.getDrawable(id)
    } catch (e: Exception) {
    }
    return setRightLeftTextDrawable(drawable)
  }

  fun setRightTextDrawablePadding(drawablePadding: Int): TitleBarView {
    mRightTextDrawablePadding = drawablePadding
    mTvRight!!.compoundDrawablePadding = mRightTextDrawablePadding
    return this
  }

  fun setRightLeftTextDrawablePadding(drawablePadding: Int): TitleBarView {
    mRightTextDrawablePadding = drawablePadding
    mTvRightLeft!!.compoundDrawablePadding = mRightTextDrawablePadding
    return this
  }

  fun setRightTextDrawableWidth(width: Int): TitleBarView {
    mRightTextDrawableWidth = width
    return setRightTextDrawable(mRightTextDrawable)
  }

  fun setRightTextDrawableHeight(height: Int): TitleBarView {
    mRightTextDrawableHeight = height
    return setRightTextDrawable(mRightTextDrawable)
  }

  fun setRightLeftTextDrawableWidth(width: Int): TitleBarView {
    mRightTextDrawableWidth = width
    return setRightLeftTextDrawable(mRightLeftTextDrawable)
  }

  fun setRightLeftTextDrawableHeight(height: Int): TitleBarView {
    mRightTextDrawableHeight = height
    return setRightLeftTextDrawable(mRightLeftTextDrawable)
  }

  fun setRightLeftTextPadding(left: Int, top: Int, right: Int, bottom: Int): TitleBarView {
    mTvRightLeft!!.setPadding(left, top, right, bottom)
    return this
  }

  fun setOnRightTextClickListener(l: OnClickListener?): TitleBarView {
    mTvRight!!.setOnClickListener(l)
    return this
  }

  fun setOnRightLeftTextClickListener(l: OnClickListener?): TitleBarView {
    mTvRightLeft!!.setOnClickListener(l)
    return this
  }

  fun setRightVisible(visible: Boolean): TitleBarView {
    mTvRight!!.visibility = if (visible) VISIBLE else GONE
    return this
  }

  fun setRightLeftVisible(visible: Boolean): TitleBarView {
    mTvRightLeft!!.visibility = if (visible) VISIBLE else GONE
    return this
  }

  fun setActionTextSize(mActionTextSize: Int): TitleBarView {
    this.mActionTextSize = mActionTextSize
    return this
  }

  fun setActionTextColor(mActionTextColor: Int): TitleBarView {
    this.mActionTextColor = ColorStateList.valueOf(mActionTextColor)
    return this
  }

  fun setActionTextColor(mActionTextColor: ColorStateList?): TitleBarView {
    this.mActionTextColor = mActionTextColor
    return this
  }

  fun setActionTextBackground(drawable: Drawable?): TitleBarView {
    mActionTextBackground = drawable
    return this
  }

  fun setActionTextBackgroundColor(color: Int): TitleBarView {
    return setActionTextBackground(ColorDrawable(color))
  }

  fun setActionTextBackgroundResource(resId: Int): TitleBarView {
    var drawable: Drawable? = null
    try {
      drawable = resources.getDrawable(resId)
    } catch (e: Exception) {
    }
    return setActionTextBackground(drawable)
  }

  @JvmOverloads
  fun addLeftAction(action: Action<*>, position: Int = -1): TitleBarView {
    val view = inflateAction(action)
    mLLayoutLeft!!.addView(view, position)
    return this
  }
  /**
   * 自定义中间部分布局
   */
  /**
   * 自定义中间部分布局
   */
  @JvmOverloads
  fun addCenterAction(action: Action<*>, position: Int = -1): TitleBarView {
    val view = inflateAction(action)
    mLLayoutCenter!!.addView(view, position)
    //        removeView(mLLayoutRight);
    return this
  }

  /**
   * 在标题栏右边添加action
   *
   * @param action
   * @param position 添加的位置
   */
  @JvmOverloads
  fun addRightAction(action: Action<*>, position: Int = -1): TitleBarView {
    val view = inflateAction(action)
    mLLayoutRight!!.addView(view, position)
    return this
  }

  /**
   * 通过action加载一个View
   *
   * @param action
   * @return
   */
  private fun inflateAction(action: Action<*>): View? {
    var view: View? = null
    val obj = action.data ?: return null
    if (obj is View) {
      view = obj
    } else if (obj is String) {
      val text = TextView(context)
      text.gravity = Gravity.CENTER
      text.text = obj
      text.setTextSize(TypedValue.COMPLEX_UNIT_PX, mActionTextSize.toFloat())
      if (mActionTextColor != null) {
        text.setTextColor(mActionTextColor)
      } else {
        text.setTextColor(DEFAULT_TEXT_COLOR)
      }
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        text.background = mActionTextBackground
      } else {
        text.setBackgroundDrawable(mActionTextBackground)
      }
      view = text
    } else if (obj is Drawable) {
      val img = ImageView(context)
      img.scaleType = ImageView.ScaleType.CENTER_CROP
      img.setImageDrawable(obj)
      view = img
    }
    view!!.setPadding(mActionPadding, 0, mActionPadding, 0)
    view.tag = action
    view.setOnClickListener(action.onClickListener)
    return view
  }

  /**
   * 添加View以及相应的动作接口
   */
  interface Action<T> {
    val data: T
    val onClickListener: OnClickListener?
  }

  inner class ImageAction : Action<Drawable?> {
    override var data: Drawable? = null
      private set
    override var onClickListener: OnClickListener? = null
      private set

    constructor(mDrawable: Drawable?, onClickListener: OnClickListener?) {
      data = mDrawable
      this.onClickListener = onClickListener
    }

    constructor(drawable: Int, onClickListener: OnClickListener?) {
      try {
        data = resources.getDrawable(drawable)
      } catch (e: Exception) {
      }
      this.onClickListener = onClickListener
    }

    constructor(drawable: Int) {
      try {
        data = resources.getDrawable(drawable)
      } catch (e: Exception) {
      }
    }

    constructor(drawable: Drawable?) {
      data = drawable
    }
  }

  inner class TextAction : Action<CharSequence> {
    override var data: CharSequence
      private set
    override var onClickListener: OnClickListener? = null
      private set

    constructor(mText: CharSequence, onClickListener: OnClickListener?) {
      data = mText
      this.onClickListener = onClickListener
    }

    constructor(mText: CharSequence) {
      data = mText
    }

    constructor(mText: Int) {
      data = resources.getText(mText)
    }

    constructor(mText: Int, onClickListener: OnClickListener?) {
      data = resources.getText(mText)
      this.onClickListener = onClickListener
    }
  }

  inner class ViewAction : Action<View> {
    override var data: View
      private set
    override var onClickListener: OnClickListener? = null
      private set

    constructor(mView: View, onClickListener: OnClickListener?) {
      data = mView
      this.onClickListener = onClickListener
    }

    constructor(mView: View) {
      data = mView
    }
  }

  private val needStatusBarHeight: Int
    /**
     * 获取真实需要的状态栏高度
     *
     * @return
     */
    private get() = if (isNeedStatusBar) statusBarHeight else 0
  private val isNormalParent: Boolean
    /**
     * 当TitleBarView的父容器为ConstraintLayout(约束布局)时TitleBarView新增的高度会变成状态栏高度2倍需做特殊处理--暂不知原因
     *
     * @return
     */
    private get() = !(parent != null && parent.javaClass.simpleName.contains("ConstraintLayout"))
  private val isNeedStatusBar: Boolean
    private get() = mIsPlusStatusHeight && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT

  fun setTitleMainTextDrawableRight(res: Int): TitleBarView {
    //        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//        mTvTitleMain.setLayoutParams(layoutParams);
//        mTvTitleMain.setCompoundDrawablePadding(dip2px(drawablePadding));
//        mTvTitleMain.setCompoundDrawablesWithIntrinsicBounds(0, 0, res, 0);
    return this
  }

  companion object {
    const val DEFAULT_STATUS_BAR_ALPHA = 102 //默认透明度--5.0以上优化半透明状态栏一致
    private const val DEFAULT_TEXT_COLOR = Color.BLACK //默认文本颜色
    private const val DEFAULT_MAIN_TEXT_SIZE = 18f //主标题size dp
    private const val DEFAULT_TEXT_SIZE = 14f //文本默认size dp
    private const val DEFAULT_SUB_TEXT_SIZE = 14f //副标题默认size dp
    private const val DEFAULT_OUT_PADDING = 12f //左右padding dp--ToolBar默认16dp
    private const val DEFAULT_CENTER_GRAVITY_LEFT_PADDING = 24f //左右padding dp--ToolBar默认32dp
    val screenWidth: Int
      /**
       * 获取屏幕宽度
       *
       * @return
       */
      get() = Resources.getSystem().displayMetrics.widthPixels
    val statusBarHeight: Int
      /**
       * 获取状态栏高度
       *
       * @return
       */
      get() = BarUtils.getStatusBarHeight()

    /**
     * 将dip或dp值转换为px值
     *
     * @param dipValue dp值
     * @return
     */
    fun dip2px(dipValue: Float): Int {
      val scale = Resources.getSystem().displayMetrics.density
      return (dipValue * scale + 0.5f).toInt()
    }

    /**
     * 判断父控件是否包含某个子View
     *
     * @param father
     * @param child
     * @return
     */
    fun hasChildView(father: ViewGroup?, child: View?): Boolean {
      var had = false
      try {
        had = father!!.indexOfChild(child) != -1
      } catch (e: Exception) {
      }
      return had
    }
  }
}
